package com.simen.hellobytes.android.widget;

import android.text.InputFilter;
import android.widget.TextView;

/**
 * author: SimenHi
 * create time: 2020-08-19 18:21
 * description:
 */
public class TextViewInputFilterUtil {

    public static void addInputFilter(TextView textView, InputFilter inputFilter){
        InputFilter[] inputFilters = textView.getFilters();

        if (inputFilters == null || inputFilters.length == 0) {
            inputFilters = new InputFilter[1];
            inputFilters[0] = inputFilter;
            textView.setFilters(inputFilters);
        }else {
            for (int index = 0; index < inputFilters.length; index++) {
                if (inputFilters[index].getClass() == inputFilter.getClass()) {
                    inputFilters[index]=inputFilter;
                    return;
                }
            }

            InputFilter[] inputFiltersNew = new InputFilter[inputFilters.length + 1];
            int index = 0;
            for (; index < inputFilters.length; index++) {
                inputFiltersNew[index] = inputFilters[index];
                inputFilters[index] = null;
            }

            inputFiltersNew[index] = inputFilter;
            textView.setFilters(inputFiltersNew);
        }
    }

    public static void setInputFilter(TextView textView, InputFilter inputFilter){
        InputFilter[] inputFilters=new InputFilter[]{inputFilter};
        textView.setFilters(inputFilters);
    }

}
